package mongodb;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.InsertManyOptions;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;


public class Test {

    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://127.0.0.1:27017"));
        MongoCollection<Document> mongoCollection = mongoClient.getDatabase("test").getCollection("userinfo");

        //mongoCollection.find().forEach((Consumer<Document>) d -> System.out.println(d));
        //Test.insertMany(mongoCollection);
        /*
        try {
            Test.insert(mongoCollection);
        }catch (Exception e){
            e.printStackTrace();
        }*/
        Test.agg(mongoCollection);

    }

    public static void agg(MongoCollection<Document> mongoCollection){
        List<Document> aggs=new ArrayList<Document>();

        Document agg1=new Document();
        Document match=new Document();
        match.append("qq","1727420286");
        agg1.append("$match",match);
        aggs.add(agg1);

        Document agg2=new Document();
        Document group=new Document();
        group.append("_id","$name");
        Document avg=new Document();
        avg.append("$avg","$age");
        group.append("avg",avg);
        agg2.append("$group",group);
        aggs.add(agg2);

        aggs.forEach((Consumer<Document>) c -> System.out.println(c.toJson()));

        mongoCollection.aggregate(aggs).forEach((Consumer<Document>) c-> System.out.println(c));
    }

    public static void insert(MongoCollection<Document> mongoCollection){
        long startTime=System.currentTimeMillis();   //获取开始时间
        Document document = new Document();
        document.append("name","huangxiaoli");
        document.append("age",25);
        document.append("sex",1);
        document.append("qq","1727420286");
        document.append("tel","17760427650");

        //id存在则不处理 不存在则插入
        mongoCollection.updateOne(new Document("_id",1),new Document("$setOnInsert",document),new UpdateOptions().upsert(true));
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间： "+(endTime-startTime)+"ms");
    }

    public static void insert2(MongoCollection<Document> mongoCollection){
        long startTime=System.currentTimeMillis();   //获取开始时间
        Document document = new Document();
        document.append("_id",9999999);
        document.append("name","huangxiaoli1111");
        document.append("age",25);
        document.append("sex",1);
        document.append("qq","1727420286");
        document.append("tel","17760427650");
        List<Document> users = new ArrayList<Document>();
        users.add(document);

        InsertManyOptions options=new InsertManyOptions();
        options.ordered(false);
        options.bypassDocumentValidation(true);
        mongoCollection.insertMany(users,options);
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间： "+(endTime-startTime)/1000+"s");
    }

    public static void insertMany(MongoCollection<Document> mongoCollection){
        List<Document> users = new ArrayList<Document>();
        for (int i=9000000;i<10000000;i++){
            Document document = new Document();
            document.append("_id",i);
            document.append("name","huangxiaoli");
            document.append("age",25);
            document.append("sex",1);
            document.append("qq","1727420286");
            document.append("tel","17760427650");
            users.add(document);
        }
        mongoCollection.insertMany(users);
    }


}
